package com.guguskins.back.controller;


import com.guguskins.back.common.annotation.Authorized;
import com.guguskins.back.entity.GuGoods;
import com.guguskins.back.entity.GuInventory;
import com.guguskins.back.entity.bo.InventoryBo;
import com.guguskins.back.entity.bo.SyntheticBo;
import com.guguskins.back.entity.vo.InventoryVo;
import com.guguskins.back.entity.vo.PageVo;
import com.guguskins.back.entity.vo.ResultVo;
import com.guguskins.back.service.GuInventoryService;
import com.guguskins.back.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mybatis-plus
 * @since 2021-05-18
 */
@RestController
@RequestMapping("/inventory")
public class GuInventoryController {

    @Autowired
    private GuInventoryService inventoryService;

    @GetMapping()
    public ResultVo getInventory(@RequestHeader(value = "token") String token,
                                 @RequestParam(value = "state", required = false, defaultValue = "1") int state,
                                 @RequestParam(value = "p", required = false, defaultValue = "1") int p,
                                 @RequestParam(value = "size", required = false, defaultValue = "10") int size,
                                 @RequestParam(value = "type",required = false)int type) {
        Long userId = JwtUtil.verifyToken(token).get("userId", Long.class);

        PageVo<InventoryVo> pageVo = inventoryService.getInventory(userId, state, p, size,type);
        return ResultVo.success(pageVo);
    }

    /**
     * 获取库存饰品
     */
    @GetMapping("/goods")
    public ResultVo getGoodsInventory(@RequestHeader(value = "token", required = false, defaultValue = "") String token,
                                      @RequestParam(value = "state", required = false, defaultValue = "1") int state,
                                      @RequestParam(value = "p", required = false, defaultValue = "1") int p,
                                      @RequestParam(value = "size", required = false, defaultValue = "10") int size) {

        if (ObjectUtils.isEmpty(token)) {
            return ResultVo.success(new PageVo<>(new ArrayList<>(), 0L));
        }
        Long userId = JwtUtil.verifyToken(token).get("userId", Long.class);

        PageVo<GuGoods> pageVo = inventoryService.getGoodsInventory(userId, state, p, size);
        return ResultVo.success(pageVo);
    }

    /**
     * 发往合同
     */
    @PostMapping("sentSynthetic")
    @Authorized
    public ResultVo sentSynthetic(@RequestHeader("token") String token,
                                  @RequestBody InventoryBo inventoryBo) {
        Long userId = JwtUtil.verifyToken(token).get("userId", Long.class);
        inventoryBo.setUserId(userId);
        inventoryService.sentSynthetic(inventoryBo);
        return ResultVo.success();
    }

    @GetMapping("syntheticList")
    @Authorized
    public ResultVo syntheticList(@RequestHeader("token") String token) {
        Long userId = JwtUtil.verifyToken(token).get("userId", Long.class);
        List<GuGoods> goods = inventoryService.syntheticList(userId);
        return ResultVo.success(goods);
    }

    /**
     * 汰换合同和成
     */
    @PostMapping("synthetic")
    @Authorized
    public ResultVo synthetic(@RequestHeader("token") String token,
                              @RequestBody SyntheticBo syntheticBo) {
        Long userId = JwtUtil.verifyToken(token).get("userId", Long.class);
        GuGoods goods = inventoryService.synthetic(userId, syntheticBo.getInventoryIds());
        return ResultVo.success(goods);
    }

    /**
     * 获取最好记录
     */
    @GetMapping("/bestSynthetic")
    public ResultVo bestSynthetic(@RequestHeader(value = "token", required = false, defaultValue = "") String token) {
        if (ObjectUtils.isEmpty(token)) {
            return ResultVo.success(new GuGoods());
        }
        Long userId = JwtUtil.verifyToken(token).get("userId", Long.class);
        GuGoods goods = inventoryService.bestSynthetic(userId);
        return ResultVo.success(goods);
    }

    /**
     * 获取箱子开奖记录
     */
    @GetMapping("/list")
    public ResultVo getList(@RequestParam(value = "boxId", required = false, defaultValue = "0") long boxId,
                            @RequestParam(value = "size", required = false, defaultValue = "10") long size) {
        List<GuGoods> goodsList = inventoryService.getList(boxId, size);
        return ResultVo.success(goodsList);
    }

    /**
     * 兑换咕币
     */
    @PostMapping("/exchangeGuCurrency")
    @Authorized
    public ResultVo exchangeGuCurrency(@RequestHeader("token") String token,
                                       @RequestBody InventoryBo inventoryBo) {
        Long userId = JwtUtil.verifyToken(token).get("userId", Long.class);
        inventoryBo.setUserId(userId);
        if (inventoryService.exchangeGuCurrency(inventoryBo)) {
            return ResultVo.success("兑换成功");
        }
        return ResultVo.fail("兑换失败");
    }

    /**
     * 提现
     */
    @PostMapping("/trade")
    @Authorized
    public ResultVo trade(@RequestHeader("token") String token,
                          @RequestBody GuInventory inventory) {
        Long userId = JwtUtil.verifyToken(token).get("userId", Long.class);
        inventory.setInventoryUserId(userId);
        if (inventoryService.trade(inventory)) {
            return ResultVo.success("提现成功");
        }
        return ResultVo.fail("提现失败");
    }

    @GetMapping("/back/page")
    public ResultVo backPage(@RequestParam(value = "p", required = false, defaultValue = "1") int p,
                             @RequestParam(value = "size", required = false, defaultValue = "10") int size,
                             @RequestParam(value = "userId", required = false, defaultValue = "0") long userId,
                             @RequestParam(value = "state", required = false, defaultValue = "-1") int state,
                             @RequestParam(value = "searchKey", required = false, defaultValue = "") String searchKey) {

        PageVo<GuInventory> guInventoryPageVo = inventoryService.backPage(p, size, userId, state, searchKey);
        System.out.println(guInventoryPageVo.toString() + "============================");
        return ResultVo.success(guInventoryPageVo, "成功");
    }

}
